<?php

namespace App\Console\Commands;

use App\Common\Constant;
use App\Model\SystemMsg;
use App\Model\UserBill;
use App\Model\UserCommissionDetail;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * 队列给会员返佣
 * Class CommissionQueue
 * @package App\Console\Commands
 */
class CommissionQueue extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'commission';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'commission queue';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     */
    public function handle()
    {
        while (true){
            try {
                DB::beginTransaction();

                $queue = UserCommissionDetail::where('status', '=', 0)->first();
                if ($queue == null){
                    DB::commit();
                    sleep(30);//每隔5秒执行一次
                    echo 'ok';
                    continue;
                }
                $user = User::where('id', $queue->relation_id)->lockForUpdate()->first();
                if (is_null($user)) {
                    DB::rollBack();
                    continue;
                }
                $model = null;
                $data = [
                    'type' => 1,
                    'income_type' => 1,
                    'user_id' => $queue->relation_id,
                    'money' => $queue->money,
                    'relation_id' => $queue->id,
                    'describe'=>'营销奖励',
                    'created_at' => Carbon::now()
                ];
                switch ($queue->relation) {
                    case 1:
                    case 2:
                        //会员
                        $model = DB::table('user_bonus_count');
                        $data['relation_user_id'] = $queue->user_id;
                        break;
                    case 3:
                    case 4:
                    case 5:
                        //业务员
                        $model = DB::table('user_salesman_bonus_count');
                        $data['relation_user_id'] = $queue->user_id;
                        break;
                    case 6:
                        //运营商
                        $model = DB::table('user_operators_bonus_count');
                        break;
                    case 7:
                        //合伙人
                        $model = DB::table('user_place_partner_bonus_count');
                        break;
                }
                if (is_null($model)) {
                    DB::rollBack();
                    continue;
                }
                $model->insert($data);
                $user->increment('currentCommission', $queue->money);
                $user->increment('rewards_all', $queue->money);
                $queue->status = 1;
                $queue->save();
                SystemMsg::add($queue->relation_id,SystemMsg::INVITE,'营销奖励','您有新的收益，请到奖励金查看！',$queue->money);
                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
                die($e->getMessage());
            }
        }
    }
}
